<?php
//模块名_控制名
class User_RightController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "user/right/";	//控制对应模板目录
	}
	
	public function indexAction(){
		$total = User_Service_Right::getByCond(array(),'count(*)','ur_id',0,0);		
		$this->view->total = $total;
		$menuArr = User_Service_Menu::getAll();
		$module = User_Service_Action::getAllModule();
		$this->view->menuArr = $menuArr;
		$this->view->module = $module;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function listAction(){
    	$ur_name = $this->_request->getParam('ur_name','');
    	$ur_module = $this->_request->getParam('ur_module','');
    	$cond =array();
    	if(!empty($ur_name)){
    		$cond['ur_name'] = $ur_name;
    	}
    	if(!empty($ur_module)){
    		$cond['ur_module'] = $ur_module;
    	}
    	$page = $this->_request->getParam('page',1);
    	$pageSize = $this->_request->getParam('pageSize');
		$page = $page?$page:1;
    	$orderby = 'ur_id';
		$result = User_Service_Right::getByCond($cond,'*',$orderby, $pageSize, ($page - 1)*$pageSize);
		
		$total = User_Service_Right::getByCond($cond,'count(*)',$orderby, 0, 0);
		$data=array('ask'=>0);
		if($total>0){
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
    }
    
    public function editAction(){
    	$ur_id = $this->_request->getParam('ur_id','');
    	$ur_name = $this->_request->getParam('ur_name','');
    	$ur_description = $this->_request->getParam('ur_description','');
    	$ur_url = $this->_request->getParam('ur_url','');
    	$ur_module = $this->_request->getParam('ur_module','');
    	$ur_icon = $this->_request->getParam('ur_icon','');
    	$um_id = $this->_request->getParam('um_id','');
    	$ur_en_name = $this->_request->getParam('ur_en_name','');

    	$row = array(
    		'um_id' => $um_id,
			'ur_name' => $ur_name,
			'ur_description' => $ur_description,
			'ur_url' => $ur_url,
			'ur_module' => $ur_module,
			'ur_icon' => $ur_icon,
			'ur_en_name' => $ur_en_name,
		);
        $error = array();
        $validateArr = array();
        $validateArr[] = array('name' => $this->lang['title'], 'value' => $row['ur_name'], 'regex' => array('require', 'length[0,255]', ));
		$validateArr[] = array('name' => $this->lang['module'], 'value' => $row['ur_module'], 'regex' => array('require', 'length[0,255]', ));
		$validateArr[] = array('name' => $this->lang['muenu'], 'value' => $row['um_id'], 'regex' => array('require',));
		$validateArr[] = array('name' => 'URL', 'value' => $row['ur_url'], 'regex' => array('require', ));
        $error = Common_Service_Validator::formValidator($validateArr);
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
        
		if($ur_id){
			$result = User_Service_Right::update($row,$ur_id);			
		}else {
			$result = User_Service_Right::add($row);
		}
// 		var_dump($result);exit;
		if($result){
    		$return = array('ask'=>1);
    	}else{
    		$return = array('ask'=>0,'error'=>array('操作失败!'),);
    	}
		die(Zend_Json::encode($return));
    }
    
    public function deleteAction(){
    	$ur_id = $this->_request->getParam('id','');
    	if(!empty($ur_id)){
    		$RightObj = new User_Service_Right();
    		$result = $RightObj->delete($ur_id);
    		$msg = array();
    		if($result){
    			$msg['ask']=1;
    		}else{
    			$msg['ask']=0;
    		}
    		echo json_encode($msg);
    	}

    }
    
    public function singleAction(){
    	$ur_id = $this->_request->getParam('id','');
    	if($ur_id){
    		$result = User_Service_Right::getByUrid($ur_id);
    		$data=array('ask'=>0);
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);				
			}
			die(Zend_Json::encode($data));
    	}
    }
    
    
    public function actionsAction(){
    	$act = $this->_request->getParam('act','');
    	$ur_id = $this->_request->getParam('urid','');	//指定的权限名ID
    	
    	if($act != 'loadData'){
			$result = User_Service_Rightactionmap::getByUrid($ur_id);	//获取 权限名与action名的对应
			$actionIdArr  = array();
			foreach ($result as $k =>$v){
				$actionIdArr[] = $v['ura_id'];
			}
			
			//获取指定权限信息
			$controllName = '';
			$moduleName = '';
			$rightObj = new User_Service_Right();
			$rightInfo = $rightObj->getByUrid($ur_id);
			if($rightInfo){
				$tmpArr = explode('/',$rightInfo['ur_url']);
				$moduleName = str_replace('-','',$tmpArr[1]);
				$controllName = str_replace('-','',$tmpArr[2]);;	//与指定权限相关的控制器名
			}
			$this->view->moduleName = $moduleName;
			$this->view->controllName = $controllName;
			$this->view->ur_id = $ur_id;
			$this->view->actionIdArr = implode(',',$actionIdArr);
    		echo Cff::renderTpl($this->tplDirectory."actions.tpl","layoutaction");
    		exit();
    	}
    	
    	$count = User_Service_Action::getByCondition(array(),'count(*)','ura_id',0,0);
    	$data=array('ask'=>0);
    	if($count>0){			
			$result = User_Service_Action::getByCondition(array(),'*','ura_module',0,0);		
			$data=array(
				'count'=>$count,
				'result'=>$result,
				'ask'=>1
			);
		}
    	die(Zend_Json::encode($data));    	
    }
    
}